# _*_ coding: utf-8 _*_
"""
@ 时间    ：2024/10/23 16:12
@ 作者    ：旺财
@ 文件    ：02 股票涨跌预测模型.py
@ 说明    ：   
"""
import tushare as ts
import pandas_ta as pdt

# 需去官网注册获取token 地址 http://tushare.org/trading.html
pro = ts.pro_api('13ee8db9779eb432f6a0b3beb47dde477abebfd5ea31e7b1f35ea6fd')
df = pro.daily(ts_code='000002.SZ', start_date='20200101', end_date='20240930')
df = df.set_index('trade_date')
print(df)

# 创建衍生变量
df['close-open'] = (df['close'] - df['open'])/df['open']    # 涨跌幅度
df['high-low'] = (df['high'] - df['low'])/df['low']         # 价格波动幅度
df['MA5'] = df['close'].rolling(5).mean()       # 移动平均线指标MA值-5日移动平均值
df['MA10'] = df['close'].rolling(10).mean()     # 移动平均线指标MA值-10日移动平均值
df.dropna(inplace=True)
df['RSI'] = pdt.rsi(df['close'], timeperiod=12)     # 相对强弱指标RSI值
df['MOM'] = pdt.mom(df['close'], timeperiod=5)      # 动量指标MOM值
df['EMA12'] = pdt.ema(df['close'], timeperiod=12)   # 指数移动平均值EMA-12日指数移动平均线
df['EMA26'] = pdt.ema(df['close'], timeperiod=26)   # 指数移动平均值EMA-26日指数移动平均线
macd_data = pdt.macd(df['close'], fastperiod=12, slowperiod=26, signalperiod=9)  # 异同移动平均线MACD值
df['MACD'] = macd_data.MACD_12_26_9
df['MACDsignal'] = macd_data.MACDs_12_26_9
df['MACDhist'] = macd_data.MACDh_12_26_9
df.dropna(inplace=True)
print(df.tail())
df.to_excel('股价数据.xlsx')